package com.geek.mq.rabbitmq.helloworld;

import com.geek.mq.rabbitmq.constant.RabbitConstant;
import com.geek.mq.rabbitmq.util.RabbitMqUtil;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeoutException;

/**
 * 消费者
 */
public class ConsumerHW {

	public static void main(String[] args) throws IOException, TimeoutException {
		Connection connection = RabbitMqUtil.getConnection();

		Channel channel = connection.createChannel();
		channel.queueDeclare(RabbitConstant.RABBITMQ_QUEUE_HELLO_WORLD, false, false, false, null);

		channel.basicConsume(RabbitConstant.RABBITMQ_QUEUE_HELLO_WORLD, new DefaultConsumer(channel){
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
				String message = new String(body);
				System.out.println("消息id："+consumerTag+",消息内容：" + message);

				//签收消息,确认消息
				//envelope.getDeliveryTag() 获取这个消息的TagId
				//false只确认签收当前的消息，设置为true的时候则代表签收该消费者所有未签收的消息
				channel.basicAck(envelope.getDeliveryTag(), false);
			}
		});

	}

}
